package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.*;
import com.example.emos.api.db.pojo.TbBusiness;
import com.example.emos.api.db.pojo.TbHardware;
import com.example.emos.api.service.HardwareService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/hardware")
@Tag(name = "HardwareController",description = "硬件接口")
public class HardwareController {
    @Autowired
    private HardwareService hardwareService;
    @GetMapping("searchAllHardware")
    @Operation(summary = "查询所有硬件")
//    @SaCheckLogin
    public R searchAllHardware() {
        ArrayList<HashMap> list = hardwareService.searchAllHardware();
        return R.ok().put("list", list);
    }
    @PostMapping("searchHardwareByPage")
    @Operation(summary = "根据状态、型号查询硬件")
    public R searchHardwareByPage(@Valid @RequestBody SearchHardwareByPageForm form) {
        if((form.getStartDate()!=null&&form.getEndDate()==null)||(form.getStartDate()==null&&form.getEndDate()!=null)){
            return R.error("startDate和endDate只能同时为空，或者不为空");
        }
        int page= form.getPage();
        int length= form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
      PageUtils pageUtils= hardwareService.searchByModelAndStatus(param);
      return R.ok().put("page", pageUtils);
    }
    @GetMapping("searchHardwareSStatusInfo")
    @Operation(summary = "硬件状态信息: 状态 1、正常  2、未激活 3、休眠 4、异常")
    public R searchHardwareSStatusInfo() {
        //所有设备数
        ArrayList<HashMap> list = hardwareService.searchAllHardware();
     long allCount=  list.size();
     //状态 1、正常  2、未激活 3、休眠 4、异常
        HashMap map1= new HashMap();
        map1.put("status","1");
        long count1=  hardwareService.searchHCount(map1);
        //状态 2、未激活
        HashMap map2= new HashMap();
        map2.put("status","2");
        long count2=  hardwareService.searchHCount(map2);
        //状态 3、休眠
        HashMap map3= new HashMap();
        map3.put("status","3");
        long count3=  hardwareService.searchHCount(map3);
        //状态 4、异常
        HashMap map4= new HashMap();
        map4.put("status","4");
        long count4=  hardwareService.searchHCount(map4);
        HashMap map= new HashMap();
        map.put("allCount",allCount);
        map.put("count_normal",count1);
        map.put("count_not_active",count2);
        map.put("count_hibernate",count3);
        map.put("count_except",count4);
        return R.ok().put("data",map);

    }
    @PostMapping("addHardwareMore")
    @Operation(summary = "批量添加硬件")
    public R addHardwareMore(@Valid @RequestBody InsertHardwareMoreForm ff) {
      InsertHardwareForm[] insertHardwareForms=  ff.getInsertHardwareForms();
        ArrayList<TbHardware> list=new ArrayList<>();
        for (InsertHardwareForm form : insertHardwareForms) {
            TbHardware tbHardware=  JSONUtil.parse(form).toBean(TbHardware.class);

          list.add(tbHardware);
        }
       int rows= hardwareService.addHardwareMore(list);
        return R.ok().put("rows", rows);
    }
    @PostMapping("addHardware")
    @Operation(summary = "添加硬件")
    public R addHardware(@Valid @RequestBody InsertHardwareForm form) {
        HashMap hashMap=  JSONUtil.parse(form).toBean(HashMap.class);
        int rows= hardwareService.addHardware(hashMap);
        return R.ok().put("rows", rows);
    }
    @PostMapping("updateHardware")
    @Operation(summary = "修改硬件")
    public R updateHardware(@Valid @RequestBody UpdateHardwareForm form) {
        HashMap hashMap=  JSONUtil.parse(form).toBean(HashMap.class);
       int rows= hardwareService.updateHardware(hashMap);
       return R.ok().put("rows", rows);
    }
    @PostMapping("deleteHardwareById")
    @Operation(summary = "删除硬件")
    public R deleteHardware(@Valid @RequestBody DeleteHardwareByIdsForm form){
      int rows=  hardwareService.deleteByIds(form.getIds());
      return R.ok().put("rows", rows);
    }
}




